const { getTypesById, getTypes, addTypes, modifyTypes, delTypes, isDel ,getTypesInsures} = require('../dao/typesDao')

//根据ID保险分类
module.exports.getTypesById = async params => {
    const data = await getTypesById(params);
    if (data[0]._id) {
        return {
            message: '获取保险分类成功',
            status: 1,
            data
        }
    } else {
        return {
            message: '获取保险分类失败',
            status: 0,
            data
        }
    }
}
//获取所有保险分类带产品
module.exports.getTypesInsures = async params => {
    const data = await getTypesInsures(params);
    return {
        message: '获取保险分类成功',
        status: 1,
        data
    }
}
//获取所有保险分类
module.exports.getTypes = async params => {
    const data = await getTypes(params);
    return {
        message: '获取保险分类成功',
        status: 1,
        data
    }
}

//新增保险分类
module.exports.addTypes = async params => {
    const data = await addTypes(params);
    if (data._id != "") {
        return {
            message: '新增保险分类成功',
            status: 1,
            data
        }
    } else {
        return {
            message: '新增保险分类失败',
            status: 0,
            data
        }
    }
}

//修改保险分类
module.exports.modifyTypes = async params => {
    const data = await modifyTypes(params);
    if (data.nModified > 0) {
        return {
            message: '保险分类修改成功',
            status: 1,
        }
    } else {
        return {
            message: '保险分类修改失败',
            status: 0
        }
    }
}

//删除保险分类
module.exports.delTypes = async params => {
    const bool = await isDel(params);
    if (!bool) {
        const data = await delTypes(params);
        if (data.nModified > 0) {
            return {
                message: '保险分类删除成功',
                status: 1
            };
        } else {
            return {
                message: '保险分类删除失败',
                status: 0
            };
        }
    }
    return {
        message: '当前保险分类下还有产品，删除分类失败',
        status: 0
    };
}